package com.cb.kfc.controller;


import com.cb.kfc.domain.Permission;
import com.cb.kfc.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

//    //删除权限
//    @RequestMapping("/deletePermission")
//    public String deletePermission(String id) throws Exception {
//        permissionService.deleteById(id);
//        return "redirect:findAll.do";
//    }
    /**
     * 删除
     * @return
     * @throws Exception
     */
    @RequestMapping("/deletePermission.do")
    public String delete(String[] ids) throws Exception{
        Integer  integer = permissionService.deletePermissionById(ids);
        return "redirect:findAll.do";
    }

//    //展示权限
//    @RequestMapping("/findById")
//    public ModelAndView findById(String id) throws Exception {
//        Permission permission=  permissionService.findById(id);
//        ModelAndView mv=new ModelAndView();
//        mv.setViewName("permission-show");
//        mv.addObject("permission",permission);
//        return mv;
//    }
    //保存权限
    @RequestMapping("/save.do")
    public String save(Permission permission) throws Exception {
        permissionService.save(permission);
        return "redirect:findAll.do";
    }
    //展示权限
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page, @RequestParam(name = "size",required = true,defaultValue = "4")Integer size) throws Exception {
        ModelAndView mv=new ModelAndView();
        List<Permission> permissionList = permissionService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(permissionList);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("permissionList",permissionList);
        mv.setViewName("permission-list");
        return mv;
    }

    @RequestMapping("/permission-add.do")
    public ModelAndView permissionAdd() throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.setViewName("permission-add");
        return mv;
    }
}
